<?php

/**
 * URL helpers
 * 
 * @package Stato
 * @subpackage view
 */
/**
 * Creates a link tag of the given <var>$label</var> using a URL created by passing <var>$url_options</var> to <var>url_for</var> helper.
 * 
 * You can also pass a string instead of an array to get a link tag that uses the 
 * value of the string as the href for the link. 
 * The <var>$html_options</var> will accept an array of html attributes for the link tag, and also 
 * accepts a <var>confirm</var> option that will add a JavaScript confirm prompt.
 * 
 * Example:
 * <code>
 * link_to('Delete this item', array('action' => 'delete', 'id' => $this->item->id),
 *                             array('confirm' => 'Are you sure ?')); 
 * </code>
 */
function link_to($label, $url_options=array(), $html_options=array())
{
    if (is_array($url_options)) $url = url_for($url_options);
    else $url = html_escape($url_options);
    
    if (isset($html_options['confirm']))
    {
        $html_options['onclick'] = "input=confirm('{$html_options['confirm']}');return input;";
        unset($html_options['confirm']);
    }
    
    return content_tag('a', $label, array_merge(array('href' => $url), $html_options));
}

/**
 * Creates a link tag unless the current request uri is the same as the link, in which case only the <var>$label</var> is returned.
 */
function link_to_unless_current($label, $url_options=array(), $html_options=array())
{
    return link_to_unless(is_current_page($url_options), $label, $url_options, $html_options);
}

/**
 * Creates a link tag unless <var>$condition</var> is true, in which case only the <var>$label</var> is returned.
 */
function link_to_unless($condition, $label, $url_options=array(), $html_options=array())
{
    if ($condition) return $label;
    else return link_to($label, $url_options, $html_options);
}

/**
 * Creates a link tag if <var>$condition</var> is true.
 */
function link_to_if($condition, $label, $url_options=array(), $html_options=array())
{
    return link_to_unless(!$condition, $label, $url_options, $html_options);
}

/**
 * Generates a form containing a single button that submits to the URL created by <var>$url_options</var>.
 * 
 * This is the safest method to ensure links that cause changes to your data are not triggered by search bots or accelerators.
 * The generated form element has a class name of button-to to allow styling of the form itself and its children.
 * This helper also accepts the <var>confirm</var> option. 
 */
function button_to($label, $url_options=array(), $html_options=array())
{
    if (is_array($url_options)) $url = url_for($url_options);
    else $url = html_escape($url_options);
    
    if (isset($html_options['confirm']))
    {
        $html_options['onclick'] = "input=confirm('{$html_options['confirm']}');return input;";
        unset($html_options['confirm']);
    }
    
    $html_options = array_merge(array('type'=>'submit', 'value'=>$label), $html_options);
    
    return "<form method=\"post\" action=\"{$url}\" class=\"button-to\"><div>"
    .tag('input', $html_options)."</div></form>";
}

/**
 * Returns a URL that has been rewritten according to the <var>$options</var> array and the defined Routes.
 * 
 * Options:
 * <ul>
 * <li><var>anchor</var>: - specifies the anchor name to be appended to the path.</li>
 * <li><var>only_path</var>: - if true, returns the relative URL.</li>
 * <li><var>trailing_slash</var>: - if true, adds a trailing slash.</li>
 * <li><var>host</var>: - overrides the default (current) host if provided.</li>
 * <li><var>protocol</var>: - overrides the default (current) protocol if provided.</li>
 * </ul>
 * 
 * Examples:
 * <code>
 * url_for(array('controller' => 'posts', 'action' => 'show', 'id' => 10)); // -> http://myhost.com/posts/show/10
 * url_for(array('controller' => 'posts', 'action' => 'list', 'only_path' => true)); // -> posts/list
 * url_for(array('controller' => 'posts', 'action' => 'list', 'anchor' => '25')); // -> posts/list#25
 * </code>  
 */
function url_for($options)
{
    return SUrlRewriter::url_for($options);
}

/**
 * Returns true if the current request uri could be generated by the given <var>$options</var>.
 */
function is_current_page($options)
{
    return SUrlRewriter::is_current_page($options);
}

/**
 * Returns true if the given <var>$controller_name</var> corresponds to the current controller.
 */
function is_current_controller($controller_name)
{
    return SUrlRewriter::is_current_controller($controller_name);
}

?>
